// RareTab.cpp : implementation file
//
#ifndef OBLIVION

#include "stdafx.h"
#include "RareTab.h"
#include "afxdialogex.h"

#include "AffixFilter.h"
#include "AffixFilterDlg.h"
#include "UtilitiesMFC.h"


// CRareTab dialog

IMPLEMENT_DYNAMIC(CRareTab, CDialogEx)

CRareTab::CRareTab(CWnd* pParent /*=NULL*/)
	: CFilterTab(CRareTab::IDD, pParent)
{
	UInt32 style = GetWindowLong(CFirst.GetSafeHwnd(), GWL_STYLE);
	if ( style & ES_WANTRETURN )
		_VMESSAGE("Rare first name box wants returns.");
	else {
		_VMESSAGE("Rare first name box does not want returns; fixing.");
		style |= ES_WANTRETURN;
		SetWindowLong(CFirst.GetSafeHwnd(), GWL_STYLE, style);
	}
	
	style = GetWindowLong(CSecond.GetSafeHwnd(), GWL_STYLE);
	if ( style & ES_WANTRETURN )
		_VMESSAGE("Rare second name box wants returns.");
	else {
		_VMESSAGE("Rare second name box does not want returns; fixing.");
		style |= ES_WANTRETURN;
		SetWindowLong(CSecond.GetSafeHwnd(), GWL_STYLE, style);
	}
}

CRareTab::~CRareTab()
{
}

void CRareTab::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RARE_FIRST, CFirst);
	DDX_Control(pDX, IDC_RARE_SECOND, CSecond);
}


BEGIN_MESSAGE_MAP(CRareTab, CDialogEx)
	ON_EN_CHANGE(IDC_RARE_FIRST, &CRareTab::OnModify)
	ON_EN_CHANGE(IDC_RARE_SECOND, &CRareTab::OnModify)
END_MESSAGE_MAP()


bool CRareTab::New()
{
	// clear lists
	CFirst.SetWindowText(_T(""));
	CSecond.SetWindowText(_T(""));

	return true;
}


bool CRareTab::Save(AffixFilter& flt) const
{
	BSStringT* strptr = NULL;

	// save first names
	flt.rareFirsts.RemoveAll();
	CString firstStr;
	CFirst.GetWindowText(firstStr);
	if ( firstStr.GetLength() > 0 ) {
		for ( int i = 0, j = 1; i < firstStr.GetLength() && j > 0; i = j+2 ) {
			j = firstStr.Find("\r\n", i);
			if ( j == -1 )
				strptr = new BSStringT(firstStr.Mid(i));
			else
				strptr = new BSStringT(firstStr.Mid(i, j-i));
			_VMESSAGE("Adding first rare name '%s' [%i, %i).", strptr->c_str(), i, j);
			flt.rareFirsts.AddTail(strptr);
		}
	}
	else
		_WARNING("AffixFilter '%s' &lt;%p&gt; has no first rare names.", flt.editorID, flt.formID);

	// save second names
	flt.rareSeconds.RemoveAll();
	CString secondStr;
	CSecond.GetWindowText(secondStr);
	if ( secondStr.GetLength() > 0 ) {
		for ( int i = 0, j = 1; i < secondStr.GetLength() && j > 0; i = j+2 ) {
			j = secondStr.Find("\r\n", i);
			if ( j == -1 )
				strptr = new BSStringT(secondStr.Mid(i));
			else
				strptr = new BSStringT(secondStr.Mid(i, j-i));
			_VMESSAGE("Saving second rare name '%s' [%i, %i).", strptr->c_str(), i, j);
			flt.rareSeconds.AddTail(strptr);
		}
	}
	else
		_WARNING("AffixFilter '%s' &lt;%p&gt; has no second rare names.", flt.editorID, flt.formID);

	return true;
}


bool CRareTab::Load(const AffixFilter& flt)
{
	if ( flt.rareFirsts.GetSize() > 0 ) {
		CString firstStr;
		NiTListIterator k = flt.rareFirsts.GetHeadPos();
		BSStringT* strptr; 
		while (k) {
			strptr = flt.rareFirsts.GetNext(k);
			if ( !strptr )
				continue;
			if ( firstStr.GetLength() > 0 )
				firstStr.Append("\r\n");
			_VMESSAGE("Loading first rare name '%s'.", strptr->c_str());
			firstStr.Append(strptr->c_str());
		}
		CFirst.SetWindowText(firstStr);
	}
	else {
		_WARNING("Filter '%s' &lt;%p&gt; does not have any first rare names.", flt.editorID.c_str(), flt.formID);
		CFirst.SetWindowText(_T(""));
	}

	if ( flt.rareSeconds.GetSize() > 0 ) {
		CString secondStr;
		NiTListIterator k = flt.rareSeconds.GetHeadPos();
		BSStringT* strptr;
		while (k) {
			strptr = flt.rareSeconds.GetNext(k);
			if ( !strptr )
				continue;
			if ( secondStr.GetLength() > 0 )
				secondStr.Append("\r\n");
			_VMESSAGE("Loading second rare name '%s'.", strptr->c_str());
			secondStr.Append(strptr->c_str());
		}
		CSecond.SetWindowText(secondStr);
	}
	else {
		_WARNING("Filter '%s' &lt;%p&gt; does not have any second rare names.", flt.editorID.c_str(), flt.formID);
		CSecond.SetWindowText(_T(""));
	}

	return true;
}


// CRareTab message handlers


void CRareTab::OnModify()
{
	if ( parent )
		parent->dlgMod(true);
}

#endif
